<template>
  <div class="app-container">
    <el-form :model="formData"
             label-width="120px"
             label-position="left">
      <el-form-item label="大学">
        <el-select v-model.number="formData.universityId"
                   placeholder="请选择大学">
          <el-option v-for="item in universityList"
                     :key="item.universityId"
                     :label="item.universityName"
                     :value="item.universityId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年份">
        <el-select v-model="formData.year"
                   placeholder="请选择年份">
          <el-option v-for="item in yearList"
                     :key="item"
                     :label="item"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批次">
        <el-select v-model="formData.volunteer"
                   placeholder="请选择批次">
          <el-option v-for="item in volunteerList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科目">
        <el-select v-model="formData.subject"
                   placeholder="请选择科目">
          <el-option key="-1"
                     label="全部"
                     value="-1"></el-option>
          <el-option v-for="item in subjectList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计划人数">
        <el-input v-model="formData.planNum"></el-input>
      </el-form-item>
      <el-form-item label="实际人数">
        <el-input v-model="formData.actualNum"></el-input>
      </el-form-item>
      <el-form-item label="录取最高分">
        <el-input v-model="formData.highScore"></el-input>
      </el-form-item>
      <!-- <el-form-item label="录取最高分位次">
        <el-input v-model="formData.highQuantile"></el-input>
      </el-form-item> -->
      <el-form-item label="录取最低分">
        <el-input v-model="formData.lowScore"></el-input>
      </el-form-item>
      <!-- <el-form-item label="录取最低分位次">
        <el-input v-model="formData.lowQuantile"></el-input>
      </el-form-item> -->
      <el-form-item label="录取批次线">
        <el-input v-model="formData.batchLine"></el-input>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary"
                 :loading="loading"
                 style="width: 200px;"
                 @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import { edit, getDetail } from '@/api/enrollment'
import { getAll } from '@/api/university'
import { errorMsg, successMsg } from '@/utils/my'

export default {
  data () {
    return {
      loading: false,
      formData: {
        id: 0,
        universityId: 0,
        year: '',
        volunteer: '',
        subject: '',
        planNum: 0,
        actualNum: 0,
        batchLine: 0,
        highScore: 0,
        lowScore: 0
      },
      universityList: [],
      yearList: [],
      volunteerList: [
        { id: "1", name: '本科一批' },
        { id: "2", name: '本科二批' },
        { id: "3", name: '高职高专' }
      ],
      subjectList: [
        { id: "1", name: '文科' },
        { id: "2", name: '理科' }
      ]
    }
  },

  created () {
    this.formData.id = this.$route.params.id
    this.genYearList()
    this.fetchData()
    this.fetchUniversityList()
  },

  methods: {

    fetchData () {
      let that = this
      getDetail({ id: that.formData.id }).then(res => {
        that.formData.universityId = res.data.universityId
        that.formData.year = res.data.enrollYear.toString()
        that.formData.volunteer = res.data.enrollVolunteer.toString()
        that.formData.subject = res.data.enrollSubject.toString()
        that.formData.planNum = res.data.enrollPlanNum
        that.formData.actualNum = res.data.enrollActualNum
        that.formData.highScore = res.data.enrollHighScore
        that.formData.lowScore = res.data.enrollLowScore
        that.formData.batchLine = res.data.enrollBatchLine
        console.log(that.formData)
      })
    },

    doSubmit () {
      let that = this, d = that.formData
      d.id = parseInt(d.id)
      d.universityId = d.universityId.toString()
      d.year = d.year.toString()
      d.volunteer = d.volunteer.toString()
      d.subject = d.subject.toString()
      d.planNum = parseInt(d.planNum)
      d.actualNum = parseInt(d.actualNum)
      d.highScore = parseInt(d.highScore)
      d.lowScore = parseInt(d.lowScore)
      d.batchLine = parseInt(d.batchLine)

      if (d.universityId.length <= 0) {
        return errorMsg('请选择大学')
      }
      if (d.year.length <= 0) {
        return errorMsg('请选择年份')
      }
      if (d.volunteer.length <= 0) {
        return errorMsg('请选择批次')
      }
      if (d.subject.length <= 0) {
        return errorMsg('请选择科目')
      }
      if (d.planNum < 0) {
        return errorMsg('请填写计划人数')
      }
      if (d.actualNum < 0) {
        return errorMsg('请填写实际人数')
      }
      if (d.highScore < 0) {
        return errorMsg('请填写录取最高分')
      }
      if (d.lowScore < 0) {
        return errorMsg('请填写录取最低分')
      }
      if (d.batchLine < 0) {
        return errorMsg('请填写录取批次线')
      }
      that.loading = true
      edit(d).then(res => {
        that.loading = false
        if (res.code != 20000) {
          return errorMsg(res.message)
        }
        successMsg(res.message)
        setTimeout(function () {
          that.$router.push({ path: '/admin/enrollment/list' })
        }, 1500)
      })
    },

    genYearList () {
      const that = this
      let d = new Date(),
        curYear = d.getFullYear()
      for (let i = 0; i < 4; i++) {
        that.yearList.push(curYear)
        curYear--
      }
    },

    fetchUniversityList () {
      const that = this
      getAll().then(res => {
        that.universityList = res.data
      })
    }

  }
}
</script>

<style>
.submit-box {
  height: 50px;
  line-height: 50px;
  text-align: center;
  padding-right: 20px;
}
</style>
